'''
@Version: 0.0.1
@Author: ider
@Date: 2020-01-09 13:30:23
@LastEditors  : ider
@LastEditTime : 2020-01-09 15:08:06
@Description: 将 v1 中正确的结果，传输到 v2,并且清理 v1 数据
'''



import pickle
import  pymongo
from util import Sea
import logging
import logging.config
import time
import zlib,sys
import numpy as np
from config import SEA_URL, MONGO_URL

logging.config.fileConfig('logging.conf')

Table_old = pymongo.MongoClient(MONGO_URL).small_world.wikipedia_category_nodes
Table_new = pymongo.MongoClient(MONGO_URL).small_world.wikipedia_category_nodes_v2

SEA_old = Sea('http://192.168.1.220:8880')
SEA_new = Sea(SEA_URL)
YC_DICT = {
    2007:150918,
    2008:143547,
    2009:147343,
    2011:160800,
    2013:159513,
    2014:207310,
    2015:209279,
    2016:203541,
    2017:193203,
    2018:195959,
    2019:197414,
}


def handle_year(year):
    for doc in Table_old.find({'year':year}):
        if not Table_new.find_one(doc):
            _,_,target_dump = SEA_old.get(doc['_id'],'small_world_target_direct')
            _,_,distance_dump = SEA_old.get(doc['_id'],'small_world_distance_direct')
            try:
                target = pickle.loads(target_dump)
                distance = pickle.loads(distance_dump)
                
                if isinstance(distance,list):
                    target,distance = distance,target

                if len(distance) < YC_DICT[year] + 10:
                    
                    logging.debug(f'S,{year},{len(distance)}/{YC_DICT[year]}')
                    SEA_new.put(doc['_id'],'small_world_target_direct',zlib.compress(pickle.dumps(target)))
                    SEA_new.put(doc['_id'],'small_world_distance_direct',zlib.compress(pickle.dumps(distance)))
                    Table_new.insert_one(doc)
                else:
                    logging.info(f'F,{year},{len(distance)}/{YC_DICT[year]}')

            except Exception as e:
                logging.exception(e)
        SEA_old.delete(doc['_id'],'small_world_target_direct')
        SEA_old.delete(doc['_id'],'small_world_distance_direct')
        Table_old.delete_one(doc)


if __name__ == '__main__':
    years = [i for i in range(2007,2020)]
    del(years[years.index(2012)])
    del(years[years.index(2010)])
    argv = sys.argv
    if len(argv) == 1:
        for year in years:
            handle_year(year)
    else:
        handle_year(int(argv[1]))
